#include "tcpserver.h"

#include <QLabel>

TcpServer::TcpServer(int state, QObject *parent) :
    QTcpServer(parent), serverState(state)
{
    connectCount = 0;
}


TcpServer::~TcpServer()
{
//    this->disconnect();
//    this->close();
//    emit delteThread();
}

void TcpServer::closeClient()
{
    emit delteThread();
}

void TcpServer::setSystemState(const SystemState &state)
{
    emit receSystemState(state);
}

/*******************************************
  功能：每当有新的TCP客户端连接时，此调用此函数，为每一个客户端开启一个新的线程服务，
       来为每一个客户端服务

  参数：socketID，当前连接过来客户端的描述
  返回值：
  ******************************************/
void TcpServer::incomingConnection(int socketID)
{
#ifdef SLCD_DEBUG
    qDebug() << connectCount << socketID << "_____________________ incomingConnection ___________________" << this->serverPort();
#endif
//    if (connectCount % 2 != 0)
//    {
//        logger()->info("TcpServer: %1 client connect to server\n", QDateTime::currentDateTime().toString("yyyy-MM-dd,hh:mm:ss"));
//    }

    ClientServiceThread *cst;
    threadList.append(cst);
    if (connectCount == 0)
    {
       cst = new ClientServiceThread(socketID, serverState, this);
    } else {
        cst = new ClientServiceThread(socketID, -1, this);
    }

    connect(cst, SIGNAL(receSetLcdIDListCmd(QMap<qint16,qint8>)), this, SIGNAL(receSetLcdIDListCmd(QMap<qint16,qint8>)));

    connect(cst, SIGNAL(receChangeLcdBright(qint16,QList<quint16>)), this, SIGNAL(receChangeLcdBright(qint16,QList<quint16>)));
    connect(cst, SIGNAL(receChangeBriLv(LcdSettingStruct)), this, SIGNAL(receChangeBriLv(LcdSettingStruct)));
    connect(cst, SIGNAL(receSlcdCollectState(QList<PanelInfoStruct>)), this, SIGNAL(receSlcdCollectState(QList<PanelInfoStruct>)));
    connect(cst, SIGNAL(receGetLcdStatus()),                this, SIGNAL(receGetLcdStatus()));

    connect(cst, SIGNAL(receLcdBackLightCtl(qint8,QList<quint16>)), this, SIGNAL(receLcdBackLightCtl(qint8,QList<quint16>)));
    connect(cst, SIGNAL(getLcdSetting()),                   this, SIGNAL(getLcdSetting()));

    connect(this, SIGNAL(sendSetLcdTraValueStatus(qint8)),  cst, SIGNAL(sendSetLcdTraValueStatus(qint8)));
    connect(this, SIGNAL(sendBrihgtness(qint16)),           cst, SIGNAL(sendBrihgtness(qint16)));
    connect(this, SIGNAL(sendLcdSetting(LcdSettingStruct)), cst, SIGNAL(sendLcdSetting(LcdSettingStruct)));

    connect(this, SIGNAL(sendBackLightStatus(qint8)),       cst, SIGNAL(sendBackLightStatus(qint8)));
    connect(this, SIGNAL(sendVideoStatus(QList<LCDSTATUS>,bool)), cst, SIGNAL(sendVideoStatus(QList<LCDSTATUS>,bool)));
    //发送系统状态（cpu,内存，硬盘使用率）
    connect(this, SIGNAL(receSystemState(SystemState)),     cst, SIGNAL(receSystemState(SystemState)));

    connect(this, SIGNAL(lcdAnswerStatus(QList<LCDSTATUS>)), cst, SIGNAL(lcdAnswerStatus(QList<LCDSTATUS>)));

    connect(cst, SIGNAL(receFreedData(FreedDogStruct)),     this, SIGNAL(receFeedData(FreedDogStruct)));
    connect(cst, SIGNAL(serviceInfoSignal(QString,int)),    this, SIGNAL(serviceInfoSignal(QString,int)));

    connect(cst, SIGNAL(updateService(int,int)),            this, SIGNAL(updateService(int,int)));
    connect(cst, SIGNAL(finished()),                        cst,  SLOT(deleteLater()));
    connect(cst, SIGNAL(finished()),                        this, SLOT(threadFinish()));
    connect(cst, SIGNAL(sendDebugMsg(QString)),             this, SIGNAL(sendDebugMsg(QString)));
    connect(cst, SIGNAL(updateConfig()),                    this, SIGNAL(updateConfig()));
    connect(cst, SIGNAL(receCheckLcdCmd(qint8)),            this, SIGNAL(receCheckLcdCmd(qint8)));

    connect(this, SIGNAL(delteThread()),                    cst, SIGNAL(serviceClose()));


    emit clientConIn();
    cst->start();
    connectCount++;
}

/*******************************************
  功能：线程结束槽函数，当一个线程结束，即调用此函数
  参数：
  返回值：
  ******************************************/
void TcpServer::threadFinish()
{
//    if (connectCount % 2 != 0)
//    {
//        logger()->info("Client:%1 connect to server\n", QDateTime::currentDateTime().toString("yyyy-MM-dd,hh:mm:ss"));
//    }

//    emit receCheckLcdCmd(0x5E);
//    qDebug() << "---------------------tcp server thread finsish---------------------------";
}
